package com.fz.controller;

import com.fz.mapper.BookMapper;
import com.fz.mybatis.MyHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by webrx on 2017-09-06.
 */
@Controller
@RequestMapping("/user")
public class Hello {
    @RequestMapping("/show")
    public void show() {
        System.out.println("hello");
    }

    @RequestMapping("")
    public void aa(HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset-utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<h3>Hello World  中文效果</h3>");
        out.flush();
        out.close();
    }

    @RequestMapping(value = "bb.action")
    public void bb(PrintWriter out) {
        out.print("hello-中国人");
    }

    @RequestMapping(value = {"c1", "c2", "c3.action"})
    public String cc() {
        return "cc";
    }

    @RequestMapping(value = "dd")
    @ResponseBody
    public String dd() {
        System.out.println("hello");
        return null;
    }

    @RequestMapping(value = "ee", method = RequestMethod.POST)
    public void ee(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset-utf-8");
        PrintWriter out = resp.getWriter();
        out.print(req.getParameter("name"));
        out.flush();
        out.close();
    }


    @RequestMapping("/ff")
    public String ff(Model m) {
        m.addAttribute("name", "李思思");
        return "ff";
    }

    @RequestMapping("/gg")
    public ModelAndView gg() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("ff");
        mv.addObject("name", "java.....");
        return mv;
    }

    @RequestMapping("/hh")
    public void hh(String name, PrintWriter out) {
        out.print(name);
    }

    @RequestMapping("/save")
    @ResponseBody
    public void save(String name, int age, String address) {
        System.out.printf("姓名：%s，年龄：%d岁，家庭地址：%s", name, age, address);
    }


    @RequestMapping(value="/add/{id}",method = RequestMethod.GET)
    public String add(@PathVariable("id") int sid, Model model){
        model.addAttribute("student_id",sid);
        return "add";
    }

    @RequestMapping(value="/addd/{name}/{c}",method = RequestMethod.GET) @ResponseBody
    public void addd(@PathVariable("c") int sid,@PathVariable("name") String sname,PrintWriter out){
        out.print(sid);
        out.print(sname);
    }


    /*@RequestMapping("/book")
    public String book(Model model){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        BookMapper bdao = ss.getMapper(BookMapper.class);
        model.addAttribute("books",bdao.queryAll());
        return "book";
    }*/

    /*@RequestMapping("/del/{id}")
    public String del(@PathVariable("id") int id){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        BookMapper bdao = ss.getMapper(BookMapper.class);
        bdao.deleteById(id);
        ss.commit();
        return "redirect:/book";
    }*/

}
